package com.cjg.winter.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.cjg.winter.entity.Admin;
import com.cjg.winter.mapper.AdminMapper;
import com.cjg.winter.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 老饼干
 * @since 2021-04-25
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Resource
    private JavaMailSender javaMailSender;
//    @Resource
//    private TemplateEngine templateEngine;

    @Override
    public Map< Integer,String> senMail(String email) {
        String mailUsername="<c995794390@163.com>";
        String s=null;
        Integer key =0;
        // 创建邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            // ;邮件主主题(可以多个);接收人;抄送人;隐秘抄送人;发送日期;上下文环境;邮件发送
            helper.setSubject("Winter,邮箱验证");
            helper.setFrom(mailUsername);
            helper.setTo(email);
//            IdUtil.
             s = RandomUtil.randomString(6);
            helper.setReplyTo(mailUsername);
            helper.setText("Winter社区确认:"+s);
            // 抄送人其实就相当于你同时发送给两个人。

        } catch (MessagingException e) {
            e.printStackTrace();
        }
        javaMailSender.send(mimeMessage);
        HashMap< Integer,String> map = new HashMap<>();
        map.put(key,s);
        key++;
        return map;

    }
}
